package com.lvhx.springboot.partitioner;

import com.google.common.base.Preconditions;
import com.lvhx.springboot.base.CurrentUnitOfWork;
import com.lvhx.springboot.springbatch.base.ActionCallback;
import com.lvhx.springboot.springbatch.base.ActionExecutor;
import com.lvhx.springboot.springbatch.base.DataConsumer;
import com.lvhx.springboot.springbatch.base.Parameters;

import java.util.Date;

/**
 * DataConsumer装饰器, 使用ActionExecutor来间接执行目标对象
 */
public class DataConsumerDecorator<T> implements DataConsumer<T> {
    private DataConsumer<T> dataConsumer;
    private ActionExecutor actionExecutor;

    public DataConsumerDecorator(DataConsumer<T> dataConsumer, ActionExecutor actionExecutor) {
        Preconditions.checkNotNull(dataConsumer, "dataConsumer不能为null");
        Preconditions.checkNotNull(actionExecutor, "actionExecutor不能为null");

        this.dataConsumer = dataConsumer;
        this.actionExecutor = actionExecutor;
    }

    @Override
    public void consumeData(final T entry, final Parameters args) {
        actionExecutor.excute(new ActionCallback() {
            @Override
            public <T> T doAction() {
                // 将参数中的生效日期放到缓存中
//                Date effDate = args.getDate(JobParameterKey.EFFECTIVE_DATE);
//                CurrentUnitOfWork.getCache().put(DataMapKey.POST_DATE, effDate);
                dataConsumer.consumeData(entry, args);
                return null;
            }
        });
    }
}
